//
//  CZEmoticonModel.swift
//  keyBoard
//
//  Created by 李伟鹏 on 16/5/22.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class CZEmoticonModel: NSObject, NSCoding {
    
    var id: String

    var chs: String?
    
    var png: String?{
        didSet {
            fullPngPath = emoticonBundlePath + "/" + id + "/" + png!
        }
    }
    
    var code: String?{
        didSet {
            // 将code转成emoji
            // 扫描器
            let scanner = NSScanner(string: code!)
            
            var result: UInt32 = 0
            // 扫描16进制的的字符串为UInt32
            scanner.scanHexInt(&result)
            
//            print("result: \(result)")
            
            let c = Character(UnicodeScalar(result))
            
            emoji = String(c)
        }
    }
    
    var fullPngPath: String?
    
    var emoji: String?
    
    init(dict: [String: String],id: String){
        self.id = id
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    override var description: String {
        get {
            return "\n\t\t 表情模型chs: \(chs), png: \(png), code: \(code)"
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        id = aDecoder.decodeObjectForKey("id")as? String ?? ""
        chs = aDecoder.decodeObjectForKey("chs") as? String
         png = aDecoder.decodeObjectForKey("png") as? String
        code = aDecoder.decodeObjectForKey("code") as? String
        emoji = aDecoder.decodeObjectForKey("emoji") as? String
        if let p = png {
            fullPngPath = emoticonBundlePath + "/" + id + "/" + p
        }
    }
    
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(id, forKey: "id")
        aCoder.encodeObject(chs, forKey: "chs")
        aCoder.encodeObject(png, forKey: "png")
        //        aCoder.encodeObject(fullPngPath, forKey: "fullPngPath")
        aCoder.encodeObject(code, forKey: "code")
        aCoder.encodeObject(emoji, forKey: "emoji")
    }
}
